<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd."/>
    <title>智能应急快速搭建平台V1.0</title>
    <script type="text/javascript" src="../../js/commonCss.js"></script>
    <script type="text/javascript" src="../../js/commonJs.js"></script>
</head>
<div class="easyui-layout" data-options="fit:true">
    <!-- Begin of toolbar -->
    <div id="wu-toolbar-permission">
        <div class="wu-toolbar-button">
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openAdd()" plain="true">添加</a>
            <!--            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openEdit()" plain="true">修改</a>-->
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="remove()" plain="true">删除</a>
        </div>
        <div class="wu-toolbar-search">
            <label>权限名称：</label><input id="search_deptName" name="search_deptName" class="wu-text" style="width:100px">
            <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="doSearch()">开始检索</a>
        </div>
    </div>
    <!-- End of toolbar -->
    <div style="overflow-x: auto; overflow-y: auto; height: 100%; width:100%;">
        <table id="permission-datagrid" class="easyui-datagrid" toolbar="#wu-toolbar-permission" style="table-layout:fixed"></table></div>
</div>
<!-- Begin of easyui-dialog -->
<div id="permission-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'"
     style="width:400px; padding:10px;">
    <form id="permission-form" method="post">
        <input type="hidden" name="pid" id="pid">
        <table>
            <tr>
                <td width="60" align="right">权限名称:</td>
                <td><input type="text" name="modelname" id="modelname" class="wu-text"/></td>

            </tr>
            <tr>
                <td width="60" align="right">权限标识:</td>
                <td><input type="text" name="permission" id="permission" class="wu-text"/></td>
            </tr>
            <tr>
                <td width="60" align="right">上级节点:</td>
                <td><input type="text" name="perShow" class="wu-text" id="perShow"/></td>
                <input type="hidden" name="parId" id="ckdparId">
            </tr>
        </table>
    </form>
</div>
<!-- End of easyui-dialog -->
<script type="text/javascript">
    $(function () {
        $("#deptName,#deptId").validatebox({
            required: true
        });
        var tInterval = window.setInterval(function () {
            if( $('#perShow').combotree().length>0){
                window.clearInterval(tInterval);
            }
        },100);
    });

    /**
     * Name 查询
     */
    function doSearch() {
        $('#permission-datagrid').treegrid('load', {paramData: JSON.stringify(searchData())});
    }

    /**
     * Name 添加记录
     */
    function add() {
        save("/permission/add");
    }

    /**
     * Name 修改记录
     */
    function edit() {
        save("/permission/update");
    }

    /**
     * Name 删除记录
     */
    function remove() {
        $.messager.confirm('信息提示', '确定要删除该记录？', function (result) {
            if (result) {
                var items = $('#permission-datagrid').treegrid('getSelections');
                var ids = [];
                $(items).each(function () {
                    ids.push(this.pid);
                });
                //alert(ids);return;
                $.ajax({
                    url: '/permission/del',
                    data: 'ids=' + ids,
                    success: function (data) {
                        if (data) {
                            $.messager.alert('信息提示', '删除成功！', 'info');
                            $('#permission-datagrid').treegrid();
                        } else {
                            $.messager.alert('信息提示', '删除失败！', 'info');
                        }
                    }
                });
            }
        });
    }

    /**
     * Name 打开添加窗口
     */
    function openAdd() {
        $('#permission-form').form('clear');
        $('#permission-dialog').dialog({
            closed: false,
            modal: true,
            title: "添加信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: add
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#permission-dialog').dialog('close');
                }
            }]
        });
        qxComboTree();
    }

    /**
     * Name 打开修改窗口
     */
    function qxEdit(id) {
        $('#permission-form').form('clear');
        var jsonArray = [];
        if (id != null && id != "") {
            var obj = {'column': "pid", 'type': 'eq', "value": id};
            jsonArray.push(obj)
        }
        $.ajax({
            type: 'POST',
            url: '/permission/find',
            dataType: 'json',
            data: {paramData: JSON.stringify(jsonArray)},
            success: function (data) {
                //alert(data.data.toString());
                $('#permission-form').form('load', data.data);
                qxComboTree();
                var checkV = ($("#ckdparId").val());
                if (checkV != null && checkV != "") {
                    $('#perShow').combotree('setValue', checkV);
                }
            }
        })
        $('#permission-dialog').dialog({
            closed: false,
            modal: true,
            title: "修改信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: edit
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#permission-dialog').dialog('close');
                }
            }]
        });
    }


    $('#permission-datagrid').treegrid({
        url: '/permission/all',
        rownumbers: true,
        animate: true,
        draggable: true,
        idField: 'pid',
        singleSelect: false,
        parentField: 'parId',
        treeField: 'modelname',
        pagination: false,
        checkOnSelect:false,
        loadMsg: '数据加载中请稍后……',
        frozenColumns: null,
        columns: [[{
            title: 'pid',
            field: 'pid',
            checkbox: true,
            align: 'center',
            width: 60
        }, {
            field: 'modelname',
            title: '权限名称',
            width: 600,
            align: 'left',
            formatter: function (value, data) {
                return '<a style="color: blue;" href="javascript:qxEdit(\''+data.pid+'\')">' + data.modelname + '</a>';
            }
        }, {
            field: 'permission',
            title: '权限标识',
            width: 340,
            align: 'left'
        }
        ]], onLoadSuccess: function (data) {
            $('#permission-datagrid').treegrid('expandAll');//全部展开树节点
        }, loadFilter: function (data) {
            var opt = $(this).data().treegrid.options;
            var idFiled, textFiled, parentField;
            if (opt.parentField) {
                idFiled = opt.idFiled || 'pid';
                textFiled = opt.textField || 'text';
                parentField = opt.parentField;
                var i, l, treeData = [],
                    tmpMap = [];
                for (i = 0, l = data.length; i < l; i++) {
                    tmpMap[data[i][idFiled]] = data[i];
                }
                for (i = 0, l = data.length; i < l; i++) {
                    if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
                        if (!tmpMap[data[i][parentField]]['children']) tmpMap[data[i][parentField]]['children'] = [];
                        data[i]['text'] = data[i][textFiled];
                        tmpMap[data[i][parentField]]['children'].push(data[i]);
                    } else {
                        data[i]['text'] = data[i][textFiled];
                        treeData.push(data[i]);
                    }
                }
                return treeData;
            }
            return data;
        }, onClickCell: function (field, row) {
            (row.state == "closed") ? $(this).treegrid('expand', row.pid) : $(this).treegrid('collapse', row.pid);
        }

    });

    function searchData() {
        var search_deptName = $("#search_deptName").val();
        var jsonArray = [];
        if (search_deptName != null && search_deptName != "") {
            var obj = {'column': "modelname", 'type': 'like', "value": search_deptName};
            jsonArray.push(obj)
        }
        return jsonArray;
    }

    function qxComboTree() {
        $.ajax({
            url: "/common/selectPermissionList",
            type: "POST",
            dataType: "json",
            success: function (data) {
                $('#perShow').combotree(
                    'loadData',
                    eval(data.msg));
            },
            error: function () {
                alert("error")
            }
        });
    }

    function save(url) {
        var t = $('#perShow').combotree('tree');	// get the tree object
        var n = t.tree('getSelected');		// get selected node
        if (n != null) {
            $("#parId").val(n.id);
        }
        var formObject = {};
        var formArray = $("#permission-form").serializeArray();
        $.each(formArray, function (i, item) {
            formObject[item.name] = item.value;
        });
        // alert(JSON.stringify(formObject));
        $.ajax({
            url: url,
            type: "post",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formObject),
            dataType: "json",
            success: function (data) {
                $.messager.alert('信息提示', '提交成功！', 'info');
                $('#permission-dialog').dialog('close');
                $('#permission-datagrid').treegrid('reload');
            },
            error: function () {
                $.messager.alert('信息提示', '提交失败！', 'info');
            },

        });
    }
</script>